<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>开关灯</title>
    <style>
        #con {
            text-align: center;
            vertical-align: middle;
        }

        #text {
            text-align: center;
            margin-top: 40px;
            font-size: 20px;
        }
    </style>
</head>
<body>
<div id="con">
    <img src="./js素材/off.png" id="img" width="300" height="288.6" onclick="on()">
    <div id="text">灯灭了!</div>
</div>
<script>
    var on_off = true;
    var obj = document.getElementById("img");
    var text = document.getElementById("text")

    function on() {
        if (on_off === true) {
            obj.src = "./js素材/on.png";
            text.innerHTML = "灯亮了！"
            text.style.color = "red"
            on_off=!on_off;
        } else {
            obj.src = "./js素材/off.png";
            text.innerHTML = "灯灭了！"
            text.style.color = "black";
            on_off=!on_off;
        }
    }
</script>
</body>
</html>